package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import client.Main;
import client.MemberAccount;
import java.awt.Dimension;

public class SystemSettings_GUI extends JFrame implements ActionListener, TableModelListener {

	MemberAccount ma = null;
	ChangePriv_GUI cpg = null;
	MemberApprovalGUI mapg=null;
	FreezeAccount_GUI fag=null;
	Container contentPane;
	Object [][] tbl = null;
	public String[] columnNames = {"User ID","Name","Frozen"};
	
	//buttons
	public JButton btnBack;
	public JButton btnBack1;
	public JButton btnBack2;
	public JButton btnChangePriv;
	public JButton btnLogOut;
	public JButton btnFreezeAccount;
	public JButton btnShowFrozeAcc;
	public JButton btnFreezeSubmit;
	public JButton btnSubmit;
	public JButton btnAproveAcc;
	
	
	//panels
	JPanel Mainpanel;
	JPanel panelFreezeAccount;
	JPanel panelChangePriv;
	JPanel panelTable;
	JPanel panelFrozenAcc;
	
	
	//labels
	JLabel lblHeader;
	JLabel lblHeaderFreezeAcc;
	JLabel lblHeaderShowComp;
	JLabel lblHeaderTable;
	
	
	public SystemSettings_GUI (){
		
		super();
		contentPane = this.getContentPane();
	    contentPane.setLayout(new BorderLayout(0, 0));
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		contentPane.add(getPanel(),BorderLayout.CENTER);
		this.setSize(500,400);
		this.setTitle("Zer Lah - System Settings");
		this.setVisible(true);
	}
	
	JPanel getPanel() 
	{	
		if (Mainpanel == null){
			Mainpanel = new JPanel();
			Mainpanel.setSize(500, 400);
			Mainpanel.setLayout(null);
			Mainpanel.add(getLabelHeader());
			Mainpanel.add(getBtnBack());
			Mainpanel.add(getBtnLogOut());
			Mainpanel.add(getBtnAproveAcc());
			Mainpanel.add(getBtnFreezeAccount());
			Mainpanel.add(getBtnChangPriv());
		}
		return Mainpanel;
	}
	
	JPanel getPanelFreezAcc() 
	{	
		if (panelFreezeAccount == null){
			panelFreezeAccount = new JPanel();
			panelFreezeAccount.setSize(500, 400);
			panelFreezeAccount.setLayout(null);
			panelFreezeAccount.add(getLabelHeaderFreezeAcc());
			panelFreezeAccount.add(getBtnBack1());
			panelFreezeAccount.add(getBtnLogOut());
			panelFreezeAccount.add(getBtnShowFreezeAcc());
			panelFreezeAccount.add(getBtnFreezeCommit());
		}
		return panelFreezeAccount;
	}
	
	JPanel getPanelFrozenAcc() 
	{	
		if (panelFrozenAcc == null){
			panelFrozenAcc = new JPanel();
			panelFrozenAcc.setSize(500, 400);
			panelFrozenAcc.setLayout(null);
			panelFrozenAcc.add(getLabelHeaderFreezeAcc());
			panelFrozenAcc.add(getTable());
			panelFrozenAcc.add(getBtnBack2());
			panelFrozenAcc.add(getBtnLogOut());
			
		}
		return panelFrozenAcc;
	}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("System Settings");
			lblHeader.setBounds(160, 100, 200, 40);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JLabel getLabelHeaderFreezeAcc()
	{
		if(lblHeaderFreezeAcc == null)
		{
			lblHeaderFreezeAcc=new JLabel();
			lblHeaderFreezeAcc.setText("Freeze Accounts");
			lblHeaderFreezeAcc.setBounds(160, 100, 200, 40);
			lblHeaderFreezeAcc.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeaderFreezeAcc;
	}
	
	JButton getBtnBack(){
		
		if (btnBack == null){
			btnBack = new JButton();
			btnBack.setBounds(380, 20, 100, 40);
			btnBack.setText("Back");
		}
		return btnBack;
	}
	
	JButton getBtnBack1(){
			
			if (btnBack1 == null){
				btnBack1 = new JButton();
				btnBack1.setBounds(380, 20, 100, 40);
				btnBack1.setText("Back");
				btnBack1.addActionListener(this);
			}
			return btnBack1;
		}
	
	JButton getBtnBack2(){
		
		if (btnBack2 == null){
			btnBack2 = new JButton();
			btnBack2.setBounds(380, 20, 100, 40);
			btnBack2.setText("Back");
			btnBack2.addActionListener(this);
		}
		return btnBack2;
	}
	
	JButton getBtnLogOut() {
		
		if (btnLogOut == null){
			btnLogOut = new JButton();
			btnLogOut.setBounds(20,20,100,40);
			btnLogOut.addActionListener(this);
			btnLogOut.setText("LogOut");
		}
		return btnLogOut;
	}
	
	JButton getBtnAproveAcc() {
		
		if(btnAproveAcc == null){
			btnAproveAcc = new JButton();
			btnAproveAcc.setBounds(170, 170, 180, 40);
			btnAproveAcc.addActionListener(this);
			btnAproveAcc.setText("Account Approving");
		}
		return btnAproveAcc;
	}
	
	JButton getBtnFreezeAccount() {
		
		if(btnFreezeAccount == null){
			btnFreezeAccount = new JButton();
			btnFreezeAccount.setBounds(170, 220, 180, 40);
			btnFreezeAccount.addActionListener(this);
			btnFreezeAccount.setText("Freeze/Unfreeze Account");
		}
		return btnFreezeAccount;
	}
	
JButton getBtnShowFreezeAcc() {
		
		if(btnShowFrozeAcc == null){
			btnShowFrozeAcc = new JButton();
			btnShowFrozeAcc.setBounds(165, 220, 180, 40);
			btnShowFrozeAcc.addActionListener(this);
			btnShowFrozeAcc.setText("Show Frozen Accounts");
		}
		return btnShowFrozeAcc;
	}

JButton getBtnFreezeCommit() {
	
	if(btnFreezeSubmit == null){
		btnFreezeSubmit = new JButton();
		btnFreezeSubmit.setBounds(165, 170, 180, 40);
		btnFreezeSubmit.addActionListener(this);
		btnFreezeSubmit.setText("Freeze/Unfreeze Account");
	}
	return btnFreezeSubmit;
}
	
JButton getBtnChangPriv() {
		
		if(btnChangePriv == null){
			btnChangePriv = new JButton();
			btnChangePriv.setBounds(170, 270, 180, 40);
			btnChangePriv.addActionListener(this);
			btnChangePriv.setText("Change Privilege");
		}
		return btnChangePriv;
	}
	
	
	void showScreen()
	{
		this.setEnabled(true);
		this.setVisible(true);
	}
	
	void hideScreen()
	{
		this.setEnabled(false);
		this.setVisible(false);
	}
	
	
	public void actionPerformed(ActionEvent e) {	
		

		if(e.getSource( )== btnAproveAcc)
		{
			//panelFreezeAccount=null;
			hideScreen();
			mapg=new MemberApprovalGUI();
			mapg.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					mapg.dispose();
					showScreen();
				}
			});
			
		}
		
		if(e.getSource() == btnLogOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
		
		if(e.getSource() == btnFreezeAccount)
		{
			panelFreezeAccount=null;
			this.getContentPane().removeAll();
			this.getContentPane().add(getPanelFreezAcc(),BorderLayout.CENTER);
			validate();
			setVisible(true);
		}
		
		if(e.getSource() == btnFreezeSubmit)
		{
			hideScreen();
			fag=new FreezeAccount_GUI();
			fag.setSize(new Dimension(500, 400));
			fag.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					fag.dispose();
					showScreen();
				}
			});
		}
		
		if(e.getSource() == btnChangePriv)
		{
			hideScreen();
			cpg=new ChangePriv_GUI();
			cpg.setSize(new Dimension(500, 400));
			cpg.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cpg.dispose();
					showScreen();
				}
			});
		}
		
		
		if(e.getSource() == btnShowFrozeAcc)
		{
			
			ma = new MemberAccount();
			tbl = ma.getFrozenAccounts();
			if(tbl.length == 0)
					JOptionPane.showMessageDialog(null, "There are no frozen accounts!");
			else
			{
				panelFrozenAcc=null;
				this.getContentPane().removeAll();
				this.getContentPane().add(getPanelFrozenAcc(),BorderLayout.CENTER);
				validate();
				setVisible(true);
			}
		
		}
			
		if(e.getSource() == btnBack1)
		{
			Mainpanel=null;
			this.getContentPane().removeAll();
			this.getContentPane().add(getPanel(),BorderLayout.CENTER);
			validate();
			setVisible(true);
		}
		
		if(e.getSource() == btnBack2)
		{
			panelFreezeAccount=null;
			this.getContentPane().removeAll();
			this.getContentPane().add(getPanelFreezAcc(),BorderLayout.CENTER);
			validate();
			setVisible(true);
		}
	}
	
	JScrollPane getTable(){
		
		  DefaultTableModel model = new DefaultTableModel(tbl, columnNames){
			  public boolean isCellEditable(int rowIndex, int mColIndex) {
			      if(mColIndex!=2)
			    	  return false;
			      return true;
			      }
		  };
		  JTable table = new JTable(model); 
		  table.getModel().addTableModelListener(this);
		  TableColumn column = table.getColumnModel().getColumn(2);
		  column.setPreferredWidth(0);
		  column.setCellEditor(new DefaultCellEditor(new JCheckBox()));
		  table.getColumnModel().getColumn(2).setCellRenderer(
					new TableCellRenderer() {
						 public Component getTableCellRendererComponent(
									JTable table, Object value, boolean isSelected,
									boolean isFocused, int row, int col) {
								boolean marked = (Boolean) value;
								JCheckBox rendererComponent = new JCheckBox();
								if (marked) {
									rendererComponent.setSelected(true);
								}
								return rendererComponent;
							}
						});
		  column = table.getColumnModel().getColumn(0);
		  column.setPreferredWidth(20);
		  column = table.getColumnModel().getColumn(1);
		  column.setPreferredWidth(20);
	      JScrollPane scroll = new JScrollPane(table); 
	      scroll.setBounds(20, 240, 450, 100);
	      table.setBounds(20, 240, 450, 100);
	      return scroll; 
		}


	public void tableChanged(TableModelEvent e) {
		TableModel model = null;
		model = (TableModel)e.getSource();
		int row = e.getFirstRow();
		int column=e.getColumn();
		Boolean data = (Boolean) model.getValueAt(row, column);
		String a = new Boolean(data).toString();
		Object uid = model.getValueAt(row, 0);
		int id = new Integer(uid.toString());
		
		if(column==2){
			ma = new MemberAccount(uid);
			ma.changeFrozen(a);
		
		if(data.equals(true))
			JOptionPane.showMessageDialog(null, "Account of User "+id+"  has been successfully frozen!");
		else
			JOptionPane.showMessageDialog(null, "Account of User "+id+" has been successfully recovered!");
		}
	}
}
